﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Rhino.Mocks;

namespace TSVNAddinUtils.Test
{
	[TestFixture]
	public class VisualStudioConfigFixture
	{
		[Test]
		public void GetInstallDirOn32bit()
		{
			string keyName1 = @"SOFTWARE\Wow6432Node\Microsoft\VisualStudio\9.0";
			string keyName2 = @"SOFTWARE\Microsoft\VisualStudio\9.0";
			string keyValue = "InstallDir";
			string installDir = @"c:\VisualStudio\";

			var registryAdapter = MockRepository.GenerateMock<RegistryAdapter>();
			registryAdapter.Expect(x => x.GetValue(keyName1, keyValue)).Return(string.Empty);
			registryAdapter.Expect(x => x.GetValue(keyName2, keyValue)).Return(installDir);

			VisualStudioConfig vsConfig = new VisualStudioConfig(registryAdapter);
			string retInstallDir = vsConfig.GetInstallDir();

			Assert.AreEqual(installDir, retInstallDir);
			registryAdapter.VerifyAllExpectations();
		}

		[Test]
		public void GetInstallDirOn64bit()
		{
			string keyName = @"SOFTWARE\Wow6432Node\Microsoft\VisualStudio\9.0";
			string keyValue = "InstallDir";
			string installDir = @"c:\VisualStudio\";

			var registryAdapter = MockRepository.GenerateMock<RegistryAdapter>();
			registryAdapter.Expect(x => x.GetValue(keyName, keyValue)).Return(installDir);

			VisualStudioConfig vsConfig = new VisualStudioConfig(registryAdapter);
			string retInstallDir = vsConfig.GetInstallDir();

			Assert.AreEqual(installDir, retInstallDir);
			registryAdapter.VerifyAllExpectations();
		}

		[Test]
		public void AddLookInFolderFor32bit()
		{
			string keyName = @"SOFTWARE\Microsoft\VisualStudio\9.0\AutomationOptions\LookInFolders";
			string addinDirectory = @"c:\TortoiseSVNAddin\";

			var registryAdapter = MockRepository.GenerateMock<RegistryAdapter>();
			registryAdapter.Stub(x => x.ExistsKey(@"SOFTWARE\Wow6432Node")).Return(false);
			registryAdapter.Expect(x => x.SetValue(keyName, addinDirectory, string.Empty));

			VisualStudioConfig vsConfig = new VisualStudioConfig(registryAdapter);

			vsConfig.AddLookInFolder(addinDirectory);

			registryAdapter.VerifyAllExpectations();
		}

		[Test]
		public void AddLookInFolderfor64bit()
		{
			string keyName = @"SOFTWARE\Wow6432Node\Microsoft\VisualStudio\9.0\AutomationOptions\LookInFolders";
			string addinDirectory = @"c:\TortoiseSVNAddin\";

			var registryAdapter = MockRepository.GenerateMock<RegistryAdapter>();
			registryAdapter.Stub(x => x.ExistsKey(@"SOFTWARE\Wow6432Node")).Return(true);
			registryAdapter.Expect(x => x.SetValue(keyName, addinDirectory, string.Empty));

			VisualStudioConfig vsConfig = new VisualStudioConfig(registryAdapter);

			vsConfig.AddLookInFolder(addinDirectory);

			registryAdapter.VerifyAllExpectations();
		}

		[Test]
		public void RemoveLookInFolderFor32bit()
		{
			string keyName = @"SOFTWARE\Microsoft\VisualStudio\9.0\AutomationOptions\LookInFolders";
			string addinDirectory = @"c:\TortoiseSVNAddin\";

			var registryAdapter = MockRepository.GenerateMock<RegistryAdapter>();
			registryAdapter.Stub(x => x.ExistsKey(@"SOFTWARE\Wow6432Node")).Return(false);
			registryAdapter.Expect(x => x.DeleteValue(keyName, addinDirectory));

			VisualStudioConfig vsConfig = new VisualStudioConfig(registryAdapter);

			vsConfig.RemoveLookInFolder(addinDirectory);

			registryAdapter.VerifyAllExpectations();
		}

		[Test]
		public void RemoveLookInFolderfor64bit()
		{
			string keyName = @"SOFTWARE\Wow6432Node\Microsoft\VisualStudio\9.0\AutomationOptions\LookInFolders";
			string addinDirectory = @"c:\TortoiseSVNAddin\";

			var registryAdapter = MockRepository.GenerateMock<RegistryAdapter>();
			registryAdapter.Stub(x => x.ExistsKey(@"SOFTWARE\Wow6432Node")).Return(true);
			registryAdapter.Expect(x => x.DeleteValue(keyName, addinDirectory));

			VisualStudioConfig vsConfig = new VisualStudioConfig(registryAdapter);

			vsConfig.RemoveLookInFolder(addinDirectory);

			registryAdapter.VerifyAllExpectations();
		}
	}
}
